package com.qf.dao.impl;

import com.qf.dao.CartDao;
import com.qf.entity.Cart;
import com.qf.entity.Goods;
import com.qf.util.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wgy
 * @version V1.0
 * @project java2402_xiaomi
 * @package com.qf.dao.impl
 * @company 千锋教育
 * @date 2024/6/12 15:15
 */
public class CartDaoImpl implements CartDao {
    private QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
    @Override
    public Cart selectByUidAndPid(int uid, int pid) {
        try {
            return qr.query("select * from tb_cart where id=? and pid=?",new BeanHandler<>(Cart.class),uid,pid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insert(Cart cart) {
        Object[] params={cart.getId(),cart.getPid(), cart.getNum(),cart.getMoney()};
        try {
            qr.update("insert into tb_cart values(?,?,?,?)",params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(Cart cart) {
        Object[] params={cart.getNum(),cart.getMoney(),cart.getId(),cart.getPid()};
        try {
            qr.update("update tb_cart set num=?,money=? where id=? and pid=?",params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Cart> selectByUid(int uid) {
        try {
            return qr.query("select c.id as uid, c.pid,c.num,c.money,tg.* from tb_cart as c inner join tb_goods tg on c.pid = tg.id\n" +
                    "where c.id=?", new ResultSetHandler<List<Cart>>() {
                @Override
                public List<Cart> handle(ResultSet rs) throws SQLException {
                    List<Cart> cartList=new ArrayList<>();
                    while(rs.next()){
                        int pid = rs.getInt("pid");
                        int num = rs.getInt("num");
                        int money=rs.getInt("money");
                        int id = rs.getInt("id");
                        String name = rs.getString("name");
                        Date pubdate=rs.getTimestamp("pubdate");
                        String picture = rs.getString("picture");
                        int price = rs.getInt("price");
                        int star = rs.getInt("star");
                        String intro = rs.getString("intro");
                        int typeid = rs.getInt("typeid");
                        Goods goods=new Goods(id,name,pubdate,picture,price,star,intro,typeid,null);
                        Cart cart=new Cart(uid,pid,num,money,goods);
                        cartList.add(cart);
                    }
                    return cartList;
                }
            }, uid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void delete(int uid, int pid) {
        try {
            qr.update("delete from tb_cart where id=? and pid=?",uid,pid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteByUid(int uid) {
        try {
            Connection conn=DataSourceUtils.getConnection();
            qr.update(conn,"delete from tb_cart where id=?",uid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
